<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html lang="en">
<head>
<base href="<%=basePath%>">
<meta charset="utf-8" />
<title></title>
<meta name="description" content="overview & stats" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="static/css/bootstrap.min.css" rel="stylesheet" />
<link href="static/css/bootstrap-responsive.min.css" rel="stylesheet" />
<!-- 下拉框 -->
<link rel="stylesheet" href="static/css/chosen.css" />
<link rel="stylesheet" href="static/css/ace.min.css" />
<link rel="stylesheet" href="static/css/ace-responsive.min.css" />
<link rel="stylesheet" href="static/css/ace-skins.min.css" />
<script type="text/javascript" src="static/js/jquery-1.7.2.js"></script>
<!--提示框-->
<script type="text/javascript" src="static/js/jquery.tips.js"></script>

<script type="text/javascript">
	top.hangge();
	$(function(){
		// 初始化公司
		showTree("company", {}, '${pd.company_id}');
		
		$("#company").chosen().change(function(){
			var value = $("#company").val();
			// 初始化部门
			showTree("depart",{companyIdForDepart:value});
			// 初始化角色
			showTree("role_id",{companyIdForRole:value});
		});
		
		
		function showTree(id, param, value){
			$.post('<%=basePath%>company/CompanyRoleTree.do',param,function(resp){
				var blank = "";
				if("company" == id) {
					blank = "选择公司";
				}else if("depart" == id){
					blank = "选择部门";
				}
				
				$("#" + id).empty();
				if(id != "role_id") {
					$("#" + id).append("<option value=''>" + blank + "</option>");
				}
				if("success" == resp.result) {
					$.each(resp.data, function(i, item) {
						var option = "<option value='" + item.id + "'>" + item.text + "</option>";
						$("#" + id).append(option);
					});
				}
				$("#" + id).trigger("liszt:updated");
            	$("#" + id).chosen();
            	
            	// 如果value不为空则表示有回显数据
            	if(value != undefined) {
                    if(id == "company" || id == "depart") {
                    	$("#" + id).attr('value',value);
                        $("#" + id).trigger("liszt:updated");
                        if(id == "company") {
                    		showTree("depart", {companyIdForDepart:value}, '${pd.depart_id}');
                    		showTree("role_id", {companyIdForRole:value}, '${pd.userroleList}');
                        }
                    }else if(id == "role_id") {
                    	value = eval('(' + value + ')');
                    	// 回显角色 
                        for(var i=0;i<value.length;i++){
                            var roleid = value[i].roleid;
                            $("#" + id +" [value='"+roleid+"']").attr('selected','selected');
                        }
                        $("#" + id).trigger("liszt:updated");
                    }
            	}
            	
			},"json")
		}
	})
	function roleyz(company,parentid){
		$.ajax({
			type: "POST",
			url: '<%=basePath%>role/CompanyRole.do',
	    	data: {companyid:company,parentid:parentid,tm:new Date().getTime()},
			dataType:'json',
			cache: false,
			success: function(map){
				 if("success" == map.result){
						$("#form1").submit();
						$("#zhongxin").hide();
						$("#zhongxin2").show();
					 }else{
						$("#roleName").css("background-color","#D16E6C");
						setTimeout("$('#roleName').val('此公司角色已存在!')",500);
					 }
			}
		});
	}
	
	//保存
	function save() {
		if ($("#roleName").val() == "") {
			$("#roleName").focus();
			return false;
		}
		if ($("#company").val() == "") {

			$("#company").tips({
				side : 3,
				msg : '请选择公司',
				bg : '#AE81FF',
				time : 3
			});
			$("#company").focus();
			return false;
		}
		roleyz($("#company").val(),$("#parent_id").val());
	}
</script>
</head>
<body>
	<form action="role/add.do" name="form1" id="form1" method="post">
		<input name="PARENT_ID" id="parent_id" value="${pd.parent_id }"
			type="hidden">
		<div id="zhongxin">
			<table>
				<tr>
					<td><select class="chzn-select" id="company" name="company" value="" data-placeholder="选择公司" title="选择公司">
					</select></td>
				</tr>
						<tr>
					<td><input type="text" name="ROLE_NAME" id="roleName"
						placeholder="这里输入名称" title="名称" /></td>
				</tr>
				<tr>
					<td style="text-align: center;"><a
						class="btn btn-mini btn-primary" onclick="save();">保存</a> <a
						class="btn btn-mini btn-danger" onclick="top.Dialog.close();">取消</a>
					</td>
				</tr>
			</table>
		</div>
	</form>

	<div id="zhongxin2" class="center" style="display: none">
		<img src="static/images/jzx.gif" style="width: 50px;" /><br />
		<h4 class="lighter block green"></h4>
	</div>
	<!-- 引入 -->
	<script src="static/1.9.1/jquery.min.js"></script>
	<script type="text/javascript">
		window.jQuery
				|| document
						.write("<script src='static/js/jquery-1.9.1.min.js'>\x3C/script>");
	</script>
	<script src="static/js/bootstrap.min.js"></script>
	<script src="static/js/ace-elements.min.js"></script>
	<script src="static/js/ace.min.js"></script>
	<script type="text/javascript" src="static/js/chosen.jquery.min.js"></script>
		<!-- 下拉框 -->
		
		<script type="text/javascript">
			$(function() {

				//单选框
				$(".chzn-select").chosen();
				$(".chzn-select-deselect").chosen({
					allow_single_deselect : true
				});

				//日期框
				$('.date-picker').datepicker();

			});
		</script>
</body>
</html>
